package com.sjzl.dao.impl;

import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import com.sjzl.dao.BaseDao;
import com.sjzl.entity.BaseEntity;

@Component("daoImpl")
public class DaoImpl<T extends BaseEntity> implements BaseDao<T> {

	private HibernateTemplate hibernateTemplate;

	@Resource
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	@SuppressWarnings("unchecked")
	public T get(Class<T> clazz, int id) {
		return (T) this.hibernateTemplate.get(clazz, id);
	}

	public void save(T entity) {
		this.hibernateTemplate.save(entity);
	}

	public void delete(T entity) {
		this.hibernateTemplate.delete(entity);
	}

	public void update(T entity) {
		this.hibernateTemplate.update(entity);
	}

	public void saveOrUpdate(T entity) {
		this.hibernateTemplate.saveOrUpdate(entity);
	}

	@SuppressWarnings("unchecked")
	public List<T> list(String hql) {
		return this.hibernateTemplate.find(hql);
	}

	@SuppressWarnings("unchecked")
	public List<T> list(String hql, Object... params) {
		return this.hibernateTemplate.find(hql, params);
	}

	public List<T> list(final String hql, final int firstResult, final int maxResults) {
		@SuppressWarnings("unchecked")
		List<T> list = this.hibernateTemplate.executeFind(new HibernateCallback() {
			
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				Query query = session.createQuery(hql);
				List<T> list2 = query.setFirstResult(firstResult)
				.setMaxResults(maxResults).list();
				return list2;
			}
		});
		return list;
	}

	public List<T> list(final String hql, final int firstResult, final int maxResults,
			final Object... params) {
		@SuppressWarnings("unchecked")
		List<T> list = this.hibernateTemplate.executeFind(new HibernateCallback() {
			
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				Query query = session.createQuery(hql);
				for (int i = 0; params != null && i < params.length; i++){
					query.setParameter(i + 1, params[i]);
				}
				List<T> list2 = query.setFirstResult(firstResult)
				.setMaxResults(maxResults).list();
				return list2;
			}
		});
		return list;
	}

	public int getTotalCount(String entityName, String where) {
		if(where == null || where.equals("")){
			return ((Long)this.hibernateTemplate.find("select count(*) from "+entityName).iterator().next()).intValue();
		}
		return ((Long)this.hibernateTemplate.find("select count(*) from "+entityName+" e "+" where e."+where).iterator().next()).intValue();
	}

	public int getTotalCount(String hql) {
		return ((Long)this.hibernateTemplate.find(hql).iterator().next()).intValue();
	}

}
